import { useReactive } from "ahooks";
import { useModel } from "umi";
import { v4 as uuidv4 } from "uuid";
import compress from "gql-compress";

const useCreateProxyServer = () => {
    const { graphqlFetchPlatform } = useModel("global");

    const state = useReactive({
        loading: false,
    });

    const createProxyServer = async (variables, callback) => {
        state.loading = true;
        variables.proxyUuid = uuidv4();
        variables.createTime = new Date().getUTCTime();
        let res = await graphqlFetchPlatform({
            query,
            variables,
        });
        callback && callback(res);
        state.loading = false;
        return res;
    };

    return { ...state, createProxyServer };
};

const query = compress(`mutation MyMutation(
    $proxyUuid: UUID!
    $proxyUrl: String!
    $proxyTitle: String
    $createTime: BigInt!
) {
    createProxyServer(
        input: {
            proxyServer: {
                proxyUuid: $proxyUuid
                proxyUrl: $proxyUrl
                proxyTitle: $proxyTitle
                createTime: $createTime
            }
        }
    ) {
        clientMutationId
    }
}`);

export default useCreateProxyServer;
